package com.zhs.gccs.activity.gccs;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.amap.api.services.core.ServiceSettings;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.gyf.immersionbar.ImmersionBar;
import com.zhs.gccs.R;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.dao.TaskGccsDataDao;
import com.zhs.gccs.dao.TaskGccsFileDao;
import com.zhs.gccs.fragment.gccs.GccsDataListFragment;
import com.zhs.gccs.fragment.gccs.GccsMapFragment;
import com.zhs.gccs.model.TabNoIconEntity;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.ToastUtils;
import com.zhs.gccs.vo.ApplyGccsTask;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GccsUserTaskMapActivity extends BaseActivity {
    public static String intent_data ="data";
    @BindView(R.id.iv_back)
    View iv_back;
    @BindView(R.id.fl_content)
    FrameLayout fl_content;
    @BindView(R.id.tab_layout)
    CommonTabLayout tab_layout;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String[] mFragmentTitles = {"项目地图", "工程措施"};
    ApplyGccsTask taskBean;
    private GeoCoder mBaiduLocationCoder;//我们定位默认用的百度的，用百度的解码器
    private TaskGccsDataDao taskGccsDataDao;
    private TaskGccsFileDao taskGccsFileDao;
    public static void start(Context context, ApplyGccsTask bean){
        Intent intent = new Intent(context, GccsUserTaskMapActivity.class);
        intent.putExtra(intent_data, bean);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gccs_user_task);
        ImmersionBar.with(this).transparentBar().statusBarDarkFont(true).init();
        ButterKnife.bind(this);
        llRoot.setPadding(0, ImmersionBar.getStatusBarHeight(this), 0, 0);
        taskGccsDataDao = new TaskGccsDataDao();
        taskGccsFileDao = new TaskGccsFileDao();
        taskBean = (ApplyGccsTask) getIntent().getSerializableExtra(intent_data);
        iv_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                toFinish();
            }
        });
        initFragments();
        initData();
    }

    private void toFinish() {
        if(taskBean!=null){
            taskGccsDataDao.deleteByPid(taskBean.getPid());
        }
        //taskGccsFileDao.clearDB();
        finish();
    }

    void initFragments() {
        mFragments = new ArrayList<>();
        mFragments.add(GccsMapFragment.newInstance(taskBean));
        mFragments.add(GccsDataListFragment.newInstance(taskBean));
        for (int i = 0; i < mFragmentTitles.length; i++) {
            mTabEntities.add(new TabNoIconEntity(
                    mFragmentTitles[i]));
        }
        tab_layout.setTabData(mTabEntities, this, R.id.fl_content, mFragments);
    }


    private void initData() {
        //高德
        ServiceSettings.updatePrivacyShow(context, true, true);
        ServiceSettings.updatePrivacyAgree(context, true);
        //百度
        LocationClient.setAgreePrivacy(true);
        mBaiduLocationCoder = GeoCoder.newInstance();
    }

    @Override
    public void onBackPressed() {
        //防止用户不注意退出
    }
    //上次按下返回键的系统时间
    private long lastBackTime = 0;
    //当前按下返回键的系统时间
    private long currentBackTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //捕获返回键按下的事件
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //获取当前系统时间的毫秒数
            currentBackTime = System.currentTimeMillis();
            //比较上次按下返回键和当前按下返回键的时间差，如果大于2秒，则提示再按一次退出
            if (currentBackTime - lastBackTime > 2 * 1000) {
                ToastUtils.toast("再按一次返回键退出");
                lastBackTime = currentBackTime;
            } else { //如果两次按下的时间差小于2秒，则退出程序
                toFinish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
